Okay, so
now we will study interesting algorithm, a quantum algorithm called
the Grover search algorithm, okay. It was proposed by quantum computation
computer scientist called Grover. All right. Now, the interesting thing about
Grover search algorithm is it can be used to solve a problem
like SAT or where n variables and time, theta two to the power n over two,
okay. And it's an interesting algorithm. It illustrates how quantum
computing works and why quantum computing
can achieve speed-ups. All right. So, and it's nice enough that I can
explain to you based on everything we have learned so far about qubits and
operations on qubits. All right. So let me set up the scene for
the Grover search algorithm. What is it? Okay.
So you are given a circuit, let's say a circuit made of an gates or
gates, not gates. So you're given some kind of, a digital circuit over let's say N inputs, and you have an output, okay. So let's call this a black box function f,
all right. So you have an output and I want to find an input to
make the output one and this is just a classical circuit,
all right. So this is just a classical circuit,
all right. Classical what we will call a
combinational circuit or a logic circuit. And if you think about it, this is
equivalent to the SAT problem, all right. But it's kind of it can
accommodate other things too. So for example, you're given an array, let's say an array where element
one element two element n and some of these elements are zeros and
some of these elements are one. So which means that some of
these elements are present and some of these elements are absent, find
an input i such that f f i equals one. So this could be any arbitrary black box function. All right. So the problem is in if you want
to do in classical computation, the best known algorithm is two to
the power n and that's very simple. This algorithm is simply run through all possibilities, right. In the quantum algorithm, we are going to
give you an algorithm that actually works in Times Square root of two to the power
n, which is two to the power n over two. So we are going to knock off a square
root out of the algorithm in using the fundamental ideas in
quantum computing, all right. Now let me give you a note
about quantum algorithms. So quantum algorithm, so
the input is going to be a black box function f implemented as a circuit
which takes an end bit input and gives me one bit output, okay. And this goes into the algorithm, okay? And outcomes. So here is the idea. So suppose f i, we know secretly is one. So i is the answer, okay. So i being n bit number is the answer. All right. So what the output is
going to be is it's going to be i with high probability and
something else wrong output and erroneous output some something else. Let's say one through i minus one,
i plus one through n, but this only happens with
very low probability. So here's the fundamental thing, quantum
computation is fundamentally randomized. So any quantum algorithm is going to give you the wrong answer, some of the time with low probability. And the probability can be made, let's say
this probability is less than one fourth. And let's say this probability is
greater than three fourth, right. So, but it gives you the right
answer the majority of time. So if you keep running it,
what you will find out is eventually in roughly like two or
three steps you will get the right answer. So the probability of repeatedly
getting the wrong answer Is negligibly small, Right? And that's the important thing. So you cannot repeatedly get the wrong
answer eventually you will get the right answer. And how do you know if
you get the right answer? Very simple, feed it to the function f and
check that, it gives an output of one. All right. So this is the nature of
quantum algorithm, all right. Okay. So let me briefly summarize
Grover search algorithm. So what is the algorithm
going to look like? So the algorithm is
going to look like this. So what I'm going to do
is the first thing I'm going to do is take this
function f with n bit input one bit output and
make it a quantum function. Which means that instead of
operating on classical bits, now this function needs
to operate on qubits. But there is a problem. The problem is if you take
the classical gates like AND, or, OR
okay these are not unitary function, these are not unitary linear maps, okay. So in quantum computation, remember
you can only apply unitary maps right. On the other hand,
not works in quantum computing. So not gate works in quantum computing
because you can always take the qubit zero map it to the qubit one. You can always take the qubit
one map it to the qubit zero. And you can think of this as unitary operation given by the matrix 01, 10. Okay. So this matrix gives you
the required unity operation. There might be a minus somewhere,
I'm missing it. AND and OR are not. And one of the main problems
is they are not invert, right? So you cannot write a function that
goes from two bits to one bit. Such a function cannot be inverted, right? So if I have an output of zero,
I don't know which two inputs produced it. Is it 01 or is it 10 or is it 00? Right.
So I do not know. So it's not invertible and only invertible
things can be done in quantum computation. But there is a trick and using this
trick I can put AND gates and OR gates into quantum gates. How do I do it? So let us say an AND
gate a quantum version of AND gate will have two inputs, okay. So this is the first input,
first qubit, this is the second qubit. And my goal is to do a quantum. And it's just a classical AND, but
it's a quantum version of the AND gate on these two inputs. What I do is I give myself a third cubit,
and this is going to be the result cubit. I'm going to write the result
in this cubit, and I'm going to set this cubit to 0, okay? As a rule, all these result
registers will be kept to 0. So, and the idea is very simple. So, the unitary operation is going
to map this whole thing back to, I'm going to recover q1 and q2, they
are going to remain unchanged, unchanged. And in the third bit, the 0th result bit, now I'm going to write the bit for
q1 and q2, okay? I'm going to take the AND of it, and this is fairly easy to see this is
going to be a unitary operation. So, for example, I have 000 will map to 000, 00, sorry, 010 Will map to 010, 100 will map to 100, 110 will map to 111, And now this is the result bit, okay? Which I have, by convention, it's
always 0, and then these are q1 and q2. So, this is one way of implementing AND. So, the other four, so
there are four more. So I have to specify what happens to 001,
011, 101, and 111, but these are garbage,
why is it garbage? Because they are violating my
convention where I promise AND only if this bit is 0, okay? And, otherwise, I just will not
promise you anything, okay? So, this is a way of implementing
a quantum version of AND, and, likewise, you can implement a quantum version of OR,
okay? And the nice idea is if you can
implement AND, OR, and NOT, so then what you can do is you can
implement any classical circuit, right? You can take any classical circuit,
f, okay? And you can make a quantum version of it,
now, the only issue is the quantum version is going to take all the inputs
of the classical inputs, okay? So if there are n bits, and
it's going to take some, you're going to have to
prepare some number of bits, k, of fresh 0 bits to record the output,
and then the final output,
you will recover all the n bits. So, all the n bits of the input
are going to be unaffected, okay? Because this is quantum computation. So, all the n cubits are going
to be unaffected, okay? These are cubits, and then there's going
to be some, the result cubits, the k of them are going to be some intermediate
values which we will ignore for now. And then finally,
there's going to be a result, okay? So, there is going to be a special result
bit which is going to hold the result, okay? So there's going to be a result cubit. So, we have to prepare some cubits for
holding intermediate value. So, if you have a circuit like this,
if you have a AND followed by an OR,
let's say followed by a NOT gate. So, you would prepare a quantum, AND. And you would prepare a fresh 0 cubit for
this quantum AND, and you would prepare a quantum OR, and you would prepare another fresh
0 cubit for this quantum OR. And, by the way, this quantum AND
is going to still output the two inputs. And it's going to have a result,
intermediate result, which is this result 1, and
then there is result 2, input 3,, right? And then input 3 is also going to come
out, and result 2 is going to come out, and then you can apply a quantum NOT,
and this doesn't need a fresh bit, because this is unitary,
and then you have result 2. So, now, you have the three bits of input,
they are going to come out and change. So, the three cubits are going
to come out and change, and your result is coming out in the register
that you prepared with a 0 upfront. All right, this is how you would implement
a classical computing box in quantum, all right? And here is the nice thing, so suppose
I have a classical circuit f, okay? With n inputs, and
I made a quantum version of it. And let's say the classical circuit,
to be more exact, let's say the classical
circuit had 2 bits, b1, b2. Now, the quantum has two cubits, q1, q2. Now, suppose, I prepared a quantum state, 00, 01, 10, 11, okay? And then I prepared a special
result register which is a 0, and I have the result register, and
the two, q1, q2 unaffected, right? So, the interesting thing
is if I take this q1, q2, take this cubit and
pass it through the quantum version. It computes the value of f for
all these four inputs simultaneously. So, there is an interesting parallelism. So, there is a parallel computation
element in quantum computing where the circuit, the single circuit,
is computing in parallel, the result for
all four configurations of q1, q2. And because I can do it
in quantum operations, the output comes out, and I will get output like 00, f00 plus 01, f01 plus 10, f10 plus 11, f11. So, f11 is the bit that
corresponds to running f, okay? So, you can do this, and this is
where the magic of quantum computing, the parallelism of quantum
computing comes up, all right? So, what is Grover search algorithm? So the first step of Grover search
algorithm is I'm given a classical box f, with n inputs, one output. I'll make a quantum version of f,
make a quantum version. But remember I need to,
every time I do this, I need to prepare,
every time I run f, I need to prepare a bunch of 0 intermediate
result registers, okay? This is the important thing
to run a quantum algorithm. If you want to run classical computations, you cannot do assignments, you cannot
rewrite a cubit with another value. Just like you can't do in a program, you
cannot say x equals 2, x equals x plus 3. So, this kind of assignment is not
possible, you can't reassign in general unless this is a unitary operation,
which it isn't, right? So, you can only do unitary operations
which is the big restriction in quantum computing. All right, so what is the next
main idea behind Grover search? So, let us assume that I
have numbers from 0, 1, all the way up to 2 to the power n, okay? So, this is all the inputs are 0. Here, 2 to the power n minus 1,
which is all the inputs are 1. And any number in the middle, okay? So let me write down, let us say f(i),
secretly we know that f(i) is 1, okay? So, i must be the answer. All right, so
if you imagine this in a vector space, so what am I going to do is I'm going
to initialize something like this. So I'm going to initialize a vector u, which is going to be uniformly 00000 + 00001 + 11111. So all nbit numbers. How many of them are there? Well, there are 2 to the power n of them. And what's the normalizing constant? It's 1 / square root of 2 to the power n,
okay? Let me call this vector u, and I'm going
to informally write things like this. So I'm going to informally
write it in two dimensions. So here is the special dimension i, and here is every other dimension because this
is a 2 to the power n dimensional space. I'm just writing every other dimension,
and let me call that e sub i, okay? So this is going to be 0 + 1
all the way plus up to i-1. I'm going to skip i, i+1 + 2 to the power n- 1, okay? So this is going to be all
the other dimensions, and I'm going to put it up like here, and with
a suitable normalization constant, okay? And u is going to look like this. So u is the vector,
which is the uniform random vector, which is a sum of all nbits. So how do I get u? Interestingly, to get u,
I am just going to do the following. I'm going to prepare
a quantum state 00000. And interestingly, I'm going to apply
the Hadamard, Gate to every bit. So I'm going to apply Hadamard gate
is going to transform 0 to 0 + 1, 1 / square root 2. So when I apply the Hadamard gate,
I'm going to get 0 + 1, Kronecker product 0 + 1, Kronecker product 0 + 1, and
I'm going to do n search. Or in this case, I'm going to
do n search Kronecker products, 1 / square root 2 to the power n, taking
all the normalization constants out. And this gives me all
possible nbit numbers. So this gives me exactly u, all right? So this is how I prepare u. So u is going to be a very
special vector in my space, okay? And here is an angle between u and
e sub i, and let me call this angle theta. So theta is the angle made between, u and e sub i. I can actually calculate this angle. So what is this angle? So this angle is going to be the dot
product of u and e sub i, okay? So the dot product of u and e sub i. And when I take the dot product, that
gives me the cosine of the angle, okay? And what is the dot product? I can calculate that. So u is basically 1 / square root
2 to the power n of this vector, all 0s plus the vector of all 1s, okay? And e sub i is very, very similar, except it's 1 / square root
2 to the power n- 1, okay? So just 1 less than that of
the vector 00 all the way, except that the vector for
i is missing, okay? Because ei is the vector of
everything other than i, let's see, everything other than i,
which secretly we know it's the result. But this is not something we know,
all right? So this is going to be the vector i, i+1 all the way up to the vector 11111,
okay? Now, i is going to be missing, so i, Missing, so what's the dot product? Well, the dot product is
just multiply term by term. So it's 1 / square root 2 to the power n, square root 2 to the power n-1 times, this is going to give me a 1 + 1 + 1 + 1,
and this is going to be 2 to
the power n-1 of these, okay? And when I simplify this,
I get 2 to the power n-1 / square root 2 to the power n square
root 2 to the power n-1. And this is going to be square root of
2 to the power n-1 by square root 2 to the power n. And this is going to be square root
1- 1 / 2 to the power n, okay? So when the dust settles, this is approximately 1- 1
/ 2 to the power n/2, okay? Okay, so this is the cos theta, cosine, oops, sorry,
cosine theta, all right? Now, this is square root 1 minus, you know
cosine theta is 1- sine squared theta. And therefore, sine squared
theta is 1 / 2 to the power n, which implies that sine theta is
1 / square root 2 to the power n. And since n is large, I can use something called a small angle approximation. Right, so
in the small angle approximation, sine theta is approximately theta
when theta is approximately 0. And therefore, here, theta will become
1 / square root 2 to the power n, which is approximately theta. Because n is large,
this is going to be a small number. I can make this more formal. All right, so
what is all of this in aid of? So here is the idea, my unknown
vector i is going to be like this. And here is my vector
ei of everything else. And here is my uniform vector u,
and here is the id angle theta. So what am I going to do? So I'm going to start with a vector w,
which is initially going to be u. Let me call this w at
time 0 is going to be u. So w is going to initially
coincide with u. At every step, what am I going to do? I'm going to use quantum operations to rotate w by angle 2 theta towards i, even though I do not know i,
I am going to, To rotate w towards i, By 2 theta radians. How is that possible, right? How is that possible? And this is the amazing idea
behind quantum algorithm, amazing trick behind the Grover search,
right? This is where you get
this amazing algorithm. This is why we study this. All right, so the idea is simply this. And why does this solve the problem? By the way, let us first understand why
is this sufficient to solve the problem. So w0 starts like this. It coincides with u every time
it makes a 2 theta progress. So what I do is I run it for roughly
until this angle becomes coincidence. So wk is going to coincide with this. So how many rotations do I need? So if I This is initially 90 degrees,
so pi by two. So I will need something like pi
by two times two theta which is roughly the number of
iterations I need to run. And this is eventually theta is going
to be one by two to the square root N. So which is going to be pi by four,
pi one by square root two to the n which is square root
two to the n times pi by four, which is theta square root two to the n. So in this many iterations in k
iterations this vector w sub i or w sub j is going to coincide
very closely with this vector i, and then I do a measurement. So here is the idea. So number one,
w sub zero is going to be u, okay. So prepare w sub zero. Number 2,4 j equals 1,2 k
steps where k is roughly going to be pi by four square root
two to the n steps, okay. So that many steps rotate w sub
i plus i by two theta radiance. And let's call this result w
sub i plus one or j plus one. This is the j iteration, all right, and eventually measure w return answer,
okay. And what we will show that what will
happen is w sub zero starts to be u and every time if this angle is theta, it
rotates by two, theta towards the answer, which is going to be i, it's going
to rotate by two theta every time. And after this many steps,
it's going to be very, very close to i. So if I make a measurement, with very high
probability, it will collapse to i and with low probability,
it will collapse to something else. So with very high probability,
You will get the right answer. So all I have to convince you now
is if I have a vector w okay, if I have and let's call this
w sub j after j iterations. This is my answer i, but
this is unknown, okay. I don't know it. This angle is theta,
this is the vector u which is zero plus one plus two to
the power n minus one. It's a uniform vector up to
a normalization constant. This is going to be isaby, which is
everything but i, so i is not included. All right. So how do I do this rotation? So here is the idea,
let's call this angle alpha, okay. So first step is I'm going to reflect w j about e j. So which means that I'm
going to make a reflection, and it's going to give me this vector and
this vector is now going to make the same angle alpha plus theta but
in the other direction, okay. So now let's call this w j
hat after reflection, okay. Number one and number two is I'm going to reflect w j hat about u this vector u. And when I reflect now this
angle is alpha plus two theta. So I get a new vector which makes
an angle alpha plus two theta with you. So therefore, this angle between w j
plus one and w j must be two theta. So reflect about you
yielding w j plus one. So these are the two operations. So first operation is take,
w j and reflect about isaby and the second operation is take w j hat
the result and reflect about u, okay. So how do you do this reflection? So reflection is actually
a unitary operation that you can do in quantum mechanics, okay. So how do I do reflection? So what is reflection? So let me briefly explain
to you what is reflection. So let's take a three-dimensional
space example, okay. So suppose I take a vector, okay, let's take this vector two comma,
three comma five, okay. And I want to reflect this
vector about the x y plane. So I want to reflect this
vector about the x y plane. So how do I reflect it? Well, if I want to reflect
about the x y plane, then x and y will become unchanged, and
the only dimension is z and z will flip. So the result of this reflection will
become two comma three comma minus five. So if I reflect around the x y plane,
and by the way, if you write x as the cubit zero,
if you write y as the cubit one, if you write z as the cubit two,
then reflecting around the x y plane is reflecting around the cubit zero plus one,
okay. Everything but two, all right, which is
simply going to take the dimension to the dimension that you are not reflecting. So Z is the dimension that I am
not reflecting and negated, okay. So this is what reflecting around
something like a plane means. So what does it mean to
reflect around isaby? So remember isaby is zero
plus one plus up to i minus one plus i plus one all
the way to n minus one, which is going to be the orthogonal
cubit to the cubit i, right? So what does it mean to reflect? So if you take a vector
w j you want to reflect, let's say w j is alpha one
times zero plus alpha two times one plus alpha i times i, okay. So let's say alpha zero zero,
alpha 11 alpha i i alpha two to the power n minus one times
two to the power n minus one. So this must be two to
the power n minus one. What's the idea here? All these dimensions which are along your
reflection plane will remain unchanged. The only dimension that will flip
is alpha i so which will become. So now you will get alpha zero times
zero plus alpha one times one. The only thing that changes is
minus alpha i times I plus, alpha two to the power n minus one
time two to the power n minus one. All right. So the only thing that changes
is the alpha i coefficient. All right. But my problem is I do not know i,
if I knew i am done right. But here's where the idea in
quantum computing comes up. I have this box f, okay. If I apply this box,
this quantum f the quantum version of f, let me apply the quantum version of f
to zero, zero, zero all the way to 111. So let me take the uniform state and
this has a name we called it u. So let me apply the quantum version of f,
to u or in this case not to u, to this vector w j. So let me apply the quantum version. Now what is going to happen? So what's going to happen is
you're going to have alpha 0|0 + alpha 2 to the power
n-1| 2 to the power n-1. So this is going to be wj, right? Now what's going to happen is you're
going to get a result register, which I'm not showing. So you're going to get alpha 0,
let's say 0 is this bit and then you're going to get the result for
it, let's say 0+ alpha 1. And then you get the original cubit 1,
and then the result for one, let's say that's also a 0, okay? And let's say i is
the only thing that's 1. So then you get plus blah, blah, blah. Now you get for alpha sub i,
you get i, okay? So, let's say everything is 0. So this is f(0), let's say this is f(1). Now let's say f(i) is 1. So now you get f(i), okay? And then everything else is
going to be alpha 2 to the power n-1| 2 to the power n-1, but
it's going to be a 0, okay? So f(2) to the power n-1. So you can always do this, okay? So this is using the quantum f box,
you get the value of f for various cubits. Now, the only thing that's
going to become a 1 is i. So let me do the following,
let me take a new unitary operation where if I have a bunch of cubits and
then I have the result. If the result is 0,
I'm going to keep it unchanged. On the other hand, if the result bit is 1,
I'm going to flip and negate it, I'm going to just negate this operation,
Okay, so the idea here is
based on this last bit, I'm going to either add a minus sign or
not. And that's going to be
a unitary operation, okay? If you apply that operation to this cubit,
what you would get is alpha 0|0. And then the result cubit
is going to be 0 k result. These are going to be the inputs, Plus, the only thing that
flips is alpha sub i and that's going to be flipping to 1, okay? Because this result is a 1, it flips and
then alpha 2 to the power n-1 and then 2 to the power n-1 and
then a 0, all right? So this is a very, very clever trick
where because I have this box f, I am able to carry out this reflection. So this reflection is
carried out by this box f. And this is also the power
of quantum computing. This box f operates in parallel over
all possible inputs at the same time. So it operates in parallel, Okay, so this is how I compute a reflection
around e sub i, all right? Now, the next idea is,
once I have reflected my vector, so this is w sub j, this is e sub i, and I did this reflection,
let's call this w sub j-hat. The next step is reflect around u. So reflect around this u vector. And remember u vector is one
by square root 2 to the n, all 0 + all 1s, if the uniform vector. Now, what's the idea here? The idea here is if I want to reflect
around u, so reflection around u, It's the same as first
apply a change of bases. So first apply change of bases, To move the vector u to simply
the vector of all zeros. Second, reflect,
About the vector of all 0s. And third, change of basis, Back. And this is called a conjugation. So you change your base back where
you now change the vector base from 000 to back to u, okay? Now this change of basis has a name,
this is simply the Hadamard operator. This reflection is very, very easy just take the coefficient
that corresponds to this cubit and keep it and flip every other cubit,
just negate every other cubit, all right? And this change of base
back is also Hadamard gate. So interestingly enough, I've just
managed to convince you that the two basic operations inside the for
loop can be done using quantum operations. So what are the two basic operations? Remember, this is w sub j. The first operation is
reflect around e sub i. And then the second operation
is reflect around u. And you get w sub j+1. And you can prove very simply that
the angle between w sub j and w sub j +1 is 2theta and this is going to be in the direction
of this unknown, but the result i. So if you do roughly theta 2 to the power
square root 2 to the power n of this, you are going to get very, very close to
the result, and then you do a measurement. And the measurement is going to be a very
high probability yield the result. And with lower probability,
it might yield the wrong answer. I can quantify what this
probability will be, but I will spare you the mathematical details. So this is very, very interesting. So Grover search instead of taking theta
2 to the power n to search on an n-bit, it now reduces it to theta
square root 2 to the n. Unfortunately, this doesn't mean that I
have solved an np complete problem, right? So I have still not solved
an np complete problem. So let me conclude this lecture
on quantum algorithms by talking a little bit about the most
important quantum algorithm so far to date,
which is short algorithm for factoring, rSA numbers. So given a number n, which is
the product of two prime factors pnq, you can in polynomial time,
Find the factors. But the catch is on a quantum computer. So this is why we are so interested in
quantum computer because efficient, Quantum computer means that rSA is broken. So efficient quantum
computer can break rSA. And because you can break rSA, Internet
security as we know it is broken, okay? And this is why quantum computers are so
interesting and so important that we do not know of a good
classical algorithm for breaking rSA. All right. Now, the interesting thing is
the factoring algorithm even though it's not known to be in P,
it's not known to be NP complete either. So the big open questions are,
big open question. Where does the power of
quantum computing really lie? So we have all polynomial time
problems on a classical computer. And if it can be solved in polynomial
time on a classical computer, you should now be more or less convinced that you can solve it in
polynomial time on a quantum computer. And you have NP and we do not know that
there are problems in this donut hole, this donut hole might be empty, but
there are some NP complete problems. Now, the big million-dollar
open question is, are there NP complete problems that are efficiently on a quantum computer? OK. And that is a very, very important and
literally a million dollar question. OK. And hopefully if you're interested, I can give you lots of pointers to books
that discuss the subject much more. Lots of pointers to the complexity theory. So there is a class called BQP which is
the class of all algorithms that can be solved in polynomial time
on a quantum computer and its relationship between classical
randomized algorithms and polynomial time algorithms
is an open problem. All right. So there is plenty of open questions in
quantum computing and there's a few very, very exciting algorithms for problems which we cannot solve
easily in classical computing. Hopefully, this gives you
an idea of the flavor of quantum computing of quantum algorithms. And I hope this was
an interesting lecture for you. And if you are listening to this for
the summer of 2018, this concludes the entire
class on algorithms. This was a really fun class for
me to teach. I totally enjoyed teaching this class. I hope it was fun and educative for
you as much as it was for me. So all the best for your future endeavors. I I really hope you enjoy learning
more about computer science and more about algorithms in general. Feel free to contact me if you
want pointers to learn more. OK. All the best. Bye.